package com.qk.management.aop;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.qk.common.Result;
import com.qk.common.enums.ParamEnum;
import com.qk.common.exception.CommonException;
import com.qk.entity.OverviewVO;
import com.qk.management.anno.Cacheable;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @author 16778
 * @date 2025/10/17 21:02
 * @description
 */
@Slf4j
@Aspect
@Component
public class RedisAop {

    @Autowired
    private StringRedisTemplate redisClient;
    private static final String redisKey = "qk:reportOverview";

    @Around("execution(* com.qk.management.service.Impl.ReportOverviewServiceImpl.reportOverview())")
    public Object aroundAdvice(ProceedingJoinPoint pjp){
        Object result = null;
        String overview = null;
        try {
            overview = redisClient.opsForValue().get(redisKey);
            if(!Objects.isNull( overview)){
                return JSONUtil.toBean(overview, OverviewVO.class);
            }
        } catch (Exception e) {
            log.error("redis异常：{}", e.getMessage());
        }
        OverviewVO vo = null;
        try {
             vo =(OverviewVO) pjp.proceed();
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
        try {
            redisClient.opsForValue().set(redisKey, JSONUtil.toJsonStr(vo));
        } catch (Exception e) {
            log.error("redis异常：{}", e.getMessage());
        }
        return vo;
    }

    @Around("@annotation(com.qk.management.anno.Cacheable)")
    public Object cachealbe(ProceedingJoinPoint pjp) throws NoSuchMethodException {
        Object returnValue = null;
        String key = null;
        long expireTime = 0L;
        TimeUnit exprireTimeUnit = null;
        try {
            Class<?> targetClass = pjp.getTarget().getClass();
            String name = pjp.getSignature().getName();
            Method method = targetClass.getMethod(name);
            Cacheable annotation = method.getAnnotation(Cacheable.class);
            key =annotation.prefix()+name;
            expireTime = annotation.expireTime();
            exprireTimeUnit = annotation.expireTimeUnit();
            String jsonStr = redisClient.opsForValue().get(key);
            if(ObjectUtil.isNotEmpty(jsonStr)){
                return JSONUtil.toBean(jsonStr,method.getReturnType());
            }
        } catch (Exception e){
            log.error("缓存异常");
        }

        try {
            returnValue = pjp.proceed();
        } catch (Throwable e) {
            throw new CommonException(ParamEnum.SERVER_ERROR);
        }
        try {
            redisClient.opsForValue().set(key,JSONUtil.toJsonStr(returnValue),expireTime,exprireTimeUnit);
        } catch (Exception e) {
            log.error("缓存异常");
        }
        return returnValue;
    }
}
